package com.googlecode.kipler.satisfiability.blocking.abox;

import java.util.Iterator;
import java.util.List;

import com.googlecode.kipler.container.dl.Individual;
import com.googlecode.kipler.container.dl.operational.AncestorCollector;
import com.googlecode.kipler.container.dl.operational.SHIQAncestorCollector;


/**
 * 
 * @author İnanç Seylan
 *
 */
public class StandardSHIQABoxDoubleBlocking extends SHIQABoxBlocking {

	protected boolean isBlockingConditionSatisfied(String indvName) {
		boolean result = false;
		Individual x = getABox().getIndividual(indvName.toString());
		if (!getABox().isRoot(x)) {
			AncestorCollector ancestorCollector = new SHIQAncestorCollector(
					getABox(), x);
			List<Individual> xAncestors = ancestorCollector.collect();
			if (xAncestors.size() >= 2) {
				Iterator<Individual> i = xAncestors.iterator();
				/*
				 * first element is guaranteed to be x's predecessor, i.e.,
				 * xPrime
				 */
				Individual xPrime = i.next();
				while (i.hasNext()) {
					/*
					 * Ancestor collector guarantees that y is different from
					 * xPrime and y is not a root node.
					 */
					Individual y = i.next();
					Individual yPrime = getPredecessor(y);
					if (yPrime != null
							&& x.hasSameConceptsAs(y)
							&& xPrime.hasSameConceptsAs(yPrime)
							&& getABox().getEdges(xPrime, x).equals(
									getABox().getEdges(yPrime, y))) {
						result = true;
						break;
					}
				}
			}
		}
		return result;
	}
}
